package study

import (
	"context"
	"encoding/json"
	"ez/apps/study/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type WordTest struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64 `json:"id" bson:"id,omitempty"`

	Point *ReadWrite `json:"point" bson:"point"` //知识点

	PointId int64 `json:"pointId" bson:"pointId"` //知识点

	Student *Student `json:"student" bson:"student"` //学生

	StudentId int64 `json:"studentId" bson:"studentId"` //学生

	Score float64 `json:"score" bson:"score"` //得分

	State int64 `json:"state" bson:"state"` //状态

	Ymd int64 `json:"ymd" bson:"ymd"` //日期

	GroupKey string `json:"groupKey" bson:"groupKey"` //批次KEY

	CreateAt time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *WordTest) DocName() string { return "WordTest" }

func (this *WordTest) GetId() int64 { return this.Id }

func (this *WordTest) SetId(id int64) { this.Id = id }

func (this *WordTest) Create() error {
	return this.GetFactory().Create(this)
}

func (this *WordTest) Replace() error {
	return this.GetFactory().Replace(this)
}

func (this *WordTest) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *WordTest) Delete() error {
	return this.GetFactory().Delete(this)
}

func (this *WordTest) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *WordTest) Destroy() error {
	return this.GetFactory().Destroy(this)
}

func (this *WordTest) ToString() string {
	return string(this.ToBytes())
}

func (this *WordTest) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *WordTest) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *WordTest) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}

func (this *WordTest) LoadPoint() {
	if this.PointId == 0 {
		return
	}
	this.Point, _ = NewReadWriteCrud().FindId(this.PointId)
}

func (this *WordTest) LoadStudent() {
	if this.StudentId == 0 {
		return
	}
	this.Student, _ = NewStudentCrud().FindId(this.StudentId)
}

func (this *WordTest) ClearRelationsBeforeSave() mgo.Doc {

	this.Point = nil

	this.Student = nil

	return this
}

func neverUsed_WordTest() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type WordTestAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *WordTest
	Session *ez.Session
}

func NewWordTestAccessControl(model *WordTest, action string, session *ez.Session) *WordTestAccessControl {
	ctrl := &WordTestAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.WordTestAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
